package com.qf.generator.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.generator.Pojo.TbUser;
import com.qf.generator.Utils.R;
import com.qf.generator.service.TbUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class UserController {
    @Autowired
    private TbUserService tbUserService;
    @RequestMapping("/getUserById")
    public R getUserById(int id){
        TbUser user = tbUserService.getUserById(id);
        if (user == null){
            return R.error("用户不存在");
        }
        return R.ok(user);
    }
    @RequestMapping("/getAllUser")
    public R getUserList(){
        return R.ok(tbUserService.getUserList());
    }
    @RequestMapping("/getAllUserByPage")
    public R getUserListByPage(int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<TbUser> userList = tbUserService.getUserList();
        PageInfo<TbUser> tbUserPageInfo = new PageInfo<>(userList);
        return R.ok(tbUserPageInfo);
    }

    @RequestMapping("/login")
    public R login(String username, String password){
        TbUser user = tbUserService.login(username,password);
        if (user == null){
            return R.error("用户不存在");
        }
        return R.ok(user);
    }
    @RequestMapping("/GetUserByMap")
    public R GetUserByMap(@RequestParam HashMap<String, String> map){
        return R.ok(tbUserService.getUserByMap(map));
    }
    @RequestMapping("/GetUserByMapv2")
    public R GetUserByMapV2(@RequestParam Map<String,Object> map){
        return R.ok(tbUserService.GetUserByMapv2(map));
    }
    @RequestMapping("/loginv2")
    public R loginv2(@RequestBody TbUser user){
        TbUser user2 = tbUserService.loginv2(user);
        if (user == null){
            return R.error("用户不存在");
        }
        return R.ok(user2);
    }
    @RequestMapping("/addUser")
    public R addUser(@RequestBody TbUser user){
        int i = tbUserService.addUser(user);
        if (i <= 0){
            return R.error("添加失败");
        }
        return R.ok();
    }
    @RequestMapping("/updateUser")
    public R updateUser(@RequestBody TbUser user){
        int i = tbUserService.updateUser(user);
        if (i <= 0){
            return R.error("修改失败");
        }
        return R.ok();
    }
    @RequestMapping("/updateUserv2")
    public R updateUserv2(@RequestBody TbUser user){
        int i = tbUserService.updateUserv2(user);
        if (i <= 0){
            return R.error("修改失败");
        }
        return R.ok();
    }
    @RequestMapping("/deleteUser")
    public R deleteUser(int id){
        int i = tbUserService.deleteUser(id);
        if (i <= 0){
            return R.error("删除失败");
        }
        return R.ok();
    }
    @RequestMapping("/deleteByList")
    public R deleteByList(@RequestParam ArrayList<Integer> list){
        int i = tbUserService.deleteUserList(list);
        if (i <= 0){
            return R.error("删除失败");
        }
        return R.ok();
    }

    @RequestMapping("/GetOrderListByID")
    public R GetOrderListByID(Integer id){
        return R.ok(tbUserService.GetOrderListByID(id));
    }

}
